/*
 * Copyright (c) 2009 Tim Mead
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/**
 * Custom control/UI for the Google Maps interface. This control provides
 * zooming, panning and map type support.
 */
package com.misoneism.fire.ui.googlemaps
{
	import com.google.maps.MapType;
	import com.google.maps.controls.ControlBase;
	import com.google.maps.controls.ControlPosition;
	import com.google.maps.interfaces.IMap;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.events.ListEvent;
	
	public class FireMapControl
			extends ControlBase
	{
		[Embed(source="../../images/zoom-in-16.png")]
		private var zoomInIcon:Class;
		
		[Embed(source="../../images/zoom-out-16.png")]
		private var zoomOutIcon:Class;
		
		[Embed(source="../../images/arrow-left-10.png")]
		private var panLeftIcon:Class;
		
		[Embed(source="../../images/arrow-right-10.png")]
		private var panRightIcon:Class;
		
		[Embed(source="../../images/arrow-up-10.png")]
		private var panUpIcon:Class;
		
		[Embed(source="../../images/arrow-down-10.png")]
		private var panDownIcon:Class;
		
		public function FireMapControl()
		{
			super(new ControlPosition(ControlPosition.ANCHOR_BOTTOM_RIGHT, 100, 35));
		}
		
		public override function initControlWithMap(map:IMap):void
		{
			// first call the base class
			super.initControlWithMap(map);
			
			// Create the pan left button
			var panLeftButton:Button = new Button();
			panLeftButton.setStyle("icon", panLeftIcon);
			panLeftButton.styleName = "chrome";
			panLeftButton.toolTip = "Pan map to the left.";
			panLeftButton.width = 18;
			panLeftButton.height = 18;
			panLeftButton.x = 19;
			panLeftButton.y = -47;
			panLeftButton.addEventListener(MouseEvent.CLICK,
				function(event:MouseEvent):void
				{
					var size:Point = map.getSize();
					map.panBy(new Point(-size.x/3, 0));
				});
			
			addChild(panLeftButton);
			
			// Create the pan down button
			var panDownButton:Button = new Button();
			panDownButton.setStyle("icon", panDownIcon);
			panDownButton.styleName = "chrome";
			panDownButton.toolTip = "Pan map downwards.";
			panDownButton.width = 18;
			panDownButton.height = 18;
			panDownButton.x = 36;
			panDownButton.y = -30;
			panDownButton.addEventListener(MouseEvent.CLICK,
				function(event:MouseEvent):void
				{
					var size:Point = map.getSize();
					map.panBy(new Point(0, size.y/3));
				});
			
			addChild(panDownButton);
			
			// Create the pan right button
			var panRightButton:Button = new Button();
			panRightButton.setStyle("icon", panRightIcon);
			panRightButton.styleName = "chrome";
			panRightButton.toolTip = "Pan map to the right.";
			panRightButton.width = 18;
			panRightButton.height = 18;
			panRightButton.x = 53;
			panRightButton.y = -47;
			panRightButton.addEventListener(MouseEvent.CLICK,
				function(event:MouseEvent):void
				{
					var size:Point = map.getSize();
					map.panBy(new Point(size.x/3, 0));
				});
			
			addChild(panRightButton);
			
			// Create the pan up button
			var panUpButton:Button = new Button();
			panUpButton.setStyle("icon", panUpIcon);
			panUpButton.styleName = "chrome";
			panUpButton.toolTip = "Pan map upwards.";
			panUpButton.width = 18;
			panUpButton.height = 18;
			panUpButton.x = 36;
			panUpButton.y = -64;
			panUpButton.addEventListener(MouseEvent.CLICK,
				function(event:MouseEvent):void
				{
					var size:Point = map.getSize();
					map.panBy(new Point(0, -size.y/3));
				});
			
			addChild(panUpButton);
			
			// Create the zoom in button
			var zoomInButton:Button = new Button();
			zoomInButton.setStyle("icon", zoomInIcon);
			zoomInButton.styleName = "chrome";
			zoomInButton.toolTip = "Zoom map in.";
			zoomInButton.width = 24;
			zoomInButton.height = 24;
			zoomInButton.x = 0;
			zoomInButton.y = -30;
			zoomInButton.addEventListener(MouseEvent.CLICK,
				function(event:MouseEvent):void
				{
					map.zoomIn();
				});
			
			addChild(zoomInButton);
			
			// Create the zoom out button
			var zoomOutButton:Button = new Button();
			zoomOutButton.setStyle("icon", zoomOutIcon);
			zoomOutButton.styleName = "chrome";
			zoomOutButton.toolTip = "Zoom map out.";
			zoomOutButton.width = 24;
			zoomOutButton.height = 24;
			zoomOutButton.x = 66;
			zoomOutButton.y = -30;
			zoomOutButton.addEventListener(MouseEvent.CLICK,
				function(event:MouseEvent):void
				{
					map.zoomOut();
				});
			
			addChild(zoomOutButton);
			
			// Create the map type selection combobox
			var mapTypeComboBox:ComboBox = new ComboBox();
			mapTypeComboBox.toolTip = "Change the display type of the map.";
			mapTypeComboBox.dataProvider =
				[{label: "Normal", value: MapType.NORMAL_MAP_TYPE},
				{label: "Satellite", value: MapType.SATELLITE_MAP_TYPE},
				{label: "Hybrid", value: MapType.HYBRID_MAP_TYPE},
				{label: "Physical", value: MapType.PHYSICAL_MAP_TYPE}];
			mapTypeComboBox.width = 90;
			mapTypeComboBox.height = 24;
			mapTypeComboBox.x = 0;
			mapTypeComboBox.y = -1;
			mapTypeComboBox.addEventListener(ListEvent.CHANGE,
				function(event:ListEvent):void
				{
					map.setMapType(mapTypeComboBox.selectedItem.value);
				});
			
			addChild(mapTypeComboBox);
		}

	}
}